package view.userview;

import dao.ActorDao;
import dao.ArtistDao;
import dao.DirectorDao;
import dao.MovieDao;
import entity.Actor;
import entity.Artist;
import entity.Director;
import entity.Movie;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import view.MainWindow;

/**
 *
 * @author Sebas
 */
public class ResultsFrame extends javax.swing.JFrame {

    String s; String c;
    DefaultListModel listModel = new DefaultListModel();
    
    public ResultsFrame(String searching, String criteria) {
        this.s = searching; this.c = criteria;
        initComponents();
        if ("Movie".equals(criteria)){
            
                MovieDao md = new MovieDao();
                List<Movie> movies = md.findMovieByName(searching);                
                if (movies != null){
                    for (int i = 0; i < movies.size(); i++){
                        listModel.addElement(movies.get(i).getMovName());                
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "There are not movies.", "Info", JOptionPane.INFORMATION_MESSAGE);
                }
        }
        
        if ("Actor".equals(criteria)){
                ActorDao ad = new ActorDao();
                List<Actor> actors = ad.findAllActorsByName(searching);              
                if (actors != null){
                    for (int i = 0; i < actors.size(); i++){
                        listModel.addElement(actors.get(i).getActName() + " " + actors.get(i).getActLastname());
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "There are no actors", "Info", JOptionPane.INFORMATION_MESSAGE);
                }       
        }
        
        
        if ("Artist".equals(criteria)){
                ArtistDao ad = new ArtistDao();
                List<Artist> artists = ad.findAllActorsByName(searching);              
                if (artists != null){
                    for (int i = 0; i < artists.size(); i++){
                        listModel.addElement(artists.get(i).getArtName() + " " + artists.get(i).getArtLastname());
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "There are no artists", "Info", JOptionPane.INFORMATION_MESSAGE);
                }       
        }
        
       if ("Director".equals(criteria)){
                DirectorDao dd = new DirectorDao();
                List<Director> directors = dd.findAllDirectorsByName(searching);              
                if (directors != null){
                    for (int i = 0; i < directors.size(); i++){
                        listModel.addElement(directors.get(i).getDirName() + " " + directors.get(i).getDirLastname());
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "There are no directors", "Info", JOptionPane.INFORMATION_MESSAGE);
                }       
        }

       if ("Genre".equals(criteria)){
                MovieDao md = new MovieDao();
                List<Movie> movies = md.findMoviesByGenre(searching);
                if (movies != null){
                    for (int i = 0; i < movies.size(); i++){
                        listModel.addElement(movies.get(i).getMovName());
                    }
                }
                else{
                    JOptionPane.showMessageDialog(null, "There are no artists", "Info", JOptionPane.INFORMATION_MESSAGE);
                }       
        }       
       
        jList2.setModel(listModel);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("General Retailers");
        setResizable(false);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sources/banner-movies.jpg"))); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Movie", "Actor", "Artist", "Director", "Genre" }));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sources/search.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel1.setText("The following are the results that match exactly with your search:");

        jLabel3.setText("If you want to see details select an item of the list and then click on \"View details\".");

        jButton2.setText("See Details");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Return to Main Window");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jList2.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jList2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane2)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(33, 33, 33)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 93, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        ArrayList<String> person = new ArrayList(); person.add("Actor"); person.add("Artist"); person.add("Director");
        ArrayList<String> other = new ArrayList(); other.add("Genre"); other.add("Movie");
       
        if (jList2.getSelectedValue() != null){
            if (person.contains(c)){
                PersonDetails pd = new PersonDetails();
                pd.setVisible(true);
                this.dispose();            

            }else{
                if(other.contains(c)){
                    MovieDetails movie = new MovieDetails();
                    movie.setVisible(true);
                    this.dispose(); 
                }    
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        MainWindow main = new MainWindow();
        main.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String searching = jTextField1.getText().toString();
        String criteria = jComboBox1.getSelectedItem().toString();
        ResultsFrame results = new ResultsFrame(searching, criteria);
        results.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
